package com.edu.tankgame;

import sun.security.mscapi.CPublicKey;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

/**
 * //坦克大战的绘图区域
 */
public class MyPanel extends JPanel  implements KeyListener , Runnable{
    //定义我的坦克
    MyHero hero = null;


    ////定义敌人坦克
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTankSize = 3;
    //定义一个Node对象存储的横纵坐标及方向
    Vector<Node> nodes = new Vector<>();


    //当子弹击中坦克就加入一个Bomb对象到bombs


    //定义一个Vector存档炸弹
    Vector<Bomb> bombs = new Vector<>();
    //定义三张图片
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public MyPanel(String key) {
        //判断记录文件存在
        //存在正常执行，文件不存在开新的游戏 key = "1";
        File file = new File(Recorder.getRecordFile());
        if(file.exists()){
            nodes = Recorder.getNodesAllEnemyTankRec();
        }else {
            System.out.println("文件不存在，只能开启新游戏");
            key = "1";
        }


        //将MyPanel对象的 enemy Tank对象赋给Recorder
        Recorder.setEnemyTanks(enemyTanks);

        //初始化自己的坦克
         hero = new MyHero(500, 100);
         //hero.setSpeed(5);

        switch (key){
            case "1":
                //初始化敌人的坦克
                for (int i = 0;i < enemyTankSize;i++){
                    EnemyTank enemyTank = new EnemyTank((100 * (1 + i)), 0);
                    enemyTank.setEnemyTanks(enemyTanks);
                    //创建了坦克就让它启动
                    new Thread(enemyTank).start();

                    //定义初始方向
                    enemyTank.setDirect(2);

                    //给坦克加入一个子弹
                    Shot shot =new Shot(enemyTank.getX()+ 20,enemyTank.getY()+60,enemyTank.getDirect());
                    //加入子弹
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }
            break;
            case "2":
                //继续上局游戏
                for (int i = 0;i < nodes.size();i++){
                    Node node = nodes.get(i);
                    EnemyTank enemyTank = new EnemyTank(node.getX(), node.getY());
                    enemyTank.setEnemyTanks(enemyTanks);
                    //创建了坦克就让它启动
                    new Thread(enemyTank).start();

                    //定义初始方向
                    enemyTank.setDirect(node.getDirect());

                    //给坦克加入一个子弹
                    Shot shot =new Shot(enemyTank.getX()+ 20,enemyTank.getY()+60,enemyTank.getDirect());
                    //加入子弹
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }
            break;
            default:
                System.out.println("你的输入有误");
        }

        //初始化三张图片
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));

        //初始化背景音乐
        new AePlayWave("D:\\java_code\\tankGame04\\src\\111.wav").start();
    }

    //编写方法记录我方击毁敌方坦克的数量（IO流）
    public void showInfo(Graphics g){
        //画出玩家的总成绩
        g.setColor(Color.black);
        Font font = new Font("宋体", Font.BOLD, 20);
        g.setFont(font);
        g.drawString("您累积击毁敌方坦克",1020,30);
        drawTank(1020,60,g,0,1);
        g.setColor(Color.black);
        g.drawString(Recorder.getAllEnemyTankNum() + "",1080,100);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //填充矩形，默认是黑色
        g.fillRect(0, 0, 1000, 750);
        showInfo(g);
        //遍历画子弹
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if(shot != null && shot.isLive == true){
                 g.setColor(Color.red);
                 g.draw3DRect(shot.x,shot.y,3,3,false);
            }else{//如果shot对象已经销毁需要拿掉
                hero.shots.remove(shot);
            }
        }

        //画子弹
//        if(hero.shot != null && hero.shot.isLive == true){
//            g.setColor(Color.red);
//            g.draw3DRect(hero.shot.x,hero.shot.y,3,3,false);
//        }

        // 如果bombs中有对象，就画出
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if(bomb.life > 6){
                g.drawImage(image1,bomb.x,bomb.y,60,60,this);
            }else if (bomb.life > 3){
                g.drawImage(image2,bomb.x,bomb.y,60,60,this);
            }else if (bomb.life > 0){
                g.drawImage(image3,bomb.x,bomb.y,60,60,this);
            }
                bomb.lifeDown();
            if(bomb.life == 0){
                bombs.remove(bomb);
            }
        }



        //画坦克-封装方法
        if(hero != null && hero.isLive) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
        }


        //画出敌人的坦克

        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //当敌人坦克是存活时就画
            if(enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
                //画出敌人子弹
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    //取出子弹
                    Shot shot = enemyTank.shots.get(j);
                    //绘制
                    if (shot.isLive) {
                        g.draw3DRect(shot.x, shot.y, 3, 3, false);
                    } else {
                        enemyTank.shots.remove(shot);
                    }
                }
            } else if (!enemyTank.isLive) {
                    enemyTanks.remove(enemyTank);
            }
        }
    }


    /**
     *
     * @param x  坦克横坐标
     * @param y  坦克纵坐标
     * @param g  画坦克的笔
     * @param direct  坦克的方向 0，1，2，3（上右下左）
     * @param type   坦克的类型
     */
    //画坦克
    public void drawTank(int x, int y ,Graphics g ,int direct,int type) {
        //根据类型画不一样颜色的坦克
        switch (type) {
            case 0://我的坦克
                g.setColor(Color.red);
            break;
            case 1://敌人坦克
                g.setColor(Color.green);
            break;
        }
        //根据方向画不同的坦克
        switch (direct) {
            case 0://向上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x +30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20) ;
                g.drawLine(x+20,y+30,x+20,y) ;
                break;
            case 1://向右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y + 30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20) ;
                g.drawLine(x+30,y+20,x+60,y+20) ;
                break;
            case 2://向下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x +30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20) ;
                g.drawLine(x+20,y+30,x+20,y +60) ;
                break;
            case 3://向左
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y + 30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20) ;
                g.drawLine(x+30,y+20,x,y +20) ;
                break;
            default:
                System.out.println("暂时不处理");
        }
    }

    //编写方法判断子弹是否击中坦克
    public void hitTank(Shot s ,Tank tank){
        switch (tank.getDirect()){
            case 0:
            case 2:
                if (s.x > tank.getX() && s.x < tank.getX() + 40
                && s.y > tank.getY() && s.y < tank.getY() + 60){
                    s.isLive = false;
                    tank.isLive = false;


                    enemyTanks.remove(tank);
                    //当我方击毁敌方坦克时。就对数据  allEnemyTankNum++
                    if(tank instanceof EnemyTank){
                        Recorder.allAllEnemyTankNum();
                    }
                    //创建BOmb对象，加入到bombs集合
                    Bomb bomb = new Bomb(tank.getX(), tank.getY());
                    bombs.add(bomb);
                }
                break;
                case 1:
                case 3:
                    if (s.x > tank.getX() && s.x < tank.getX() + 60
                            && s.y > tank.getY() && s.y < tank.getY() + 40){
                        s.isLive = false;
                        tank.isLive = false;
                        enemyTanks.remove(tank);
                        //当我方击毁敌方坦克时。就对数据  allEnemyTankNum++
                        if(tank instanceof EnemyTank){
                            Recorder.allAllEnemyTankNum();
                        }
                        //创建BOmb对象，加入到bombs集合
                        Bomb bomb = new Bomb(tank.getX(), tank.getY());
                        bombs.add(bomb);
                    }
        }
    }



    //如果我们的坦克会发射多颗子弹
    //再判断我方的坦克是否都击中坦克，就需要把子弹集合
    //所有子弹中都取出和敌人坦克，进行判断
    public void hitEnemyTank(){
        //遍历我们的子弹
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && hero.shot.isLive) {
                for (int j = 0; j < enemyTanks.size(); j++) {
                    EnemyTank enemyTank = enemyTanks.get(j);
                    hitTank(hero.shot, enemyTank);
                }
            }
        }
    }

    //编写方法，判断敌人坦克是否击中我的坦克
    public void hitHero(){
        //遍历敌人坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //遍历enemyTank坦克的所有子弹
            for (int j = 0; j < enemyTank.shots.size(); j++) {
                Shot shot = enemyTank.shots.get(j);
                //判断敌人坦克子弹是否击中
                if(hero.isLive && shot.isLive){
                    hitTank(shot,hero);
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    //处理按下wdsa按钮的响应
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            if(hero.getY() > 0) {
                hero.moveUp();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            if(hero.getX() + 60  < 1000) {
                hero.moveRight();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            if(hero.getY() + 60  < 750) {
                hero.moveDown();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            if(hero.getX() > 0) {
                hero.moveLeft();
            }
        }
        //如果用户按下J，则射出子弹
        if(e.getKeyCode() == KeyEvent.VK_J){
            //判断hero的子弹是否销毁，是则创建一个新的 判断一颗子弹
//            if(hero.shot ==null || !hero.shot.isLive) {
//                hero.ShotEnemyTank();
//            }
            //多颗子弹
            hero.ShotEnemyTank();
        }

        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //判断是否击中了敌人坦克
//            if (hero.shot != null && hero.shot.isLive) {
//                for (int i = 0; i < enemyTanks.size(); i++) {
//                    EnemyTank enemyTank = enemyTanks.get(i);
//                    hitTank(hero.shot, enemyTank);
//                }
//            }
            hitEnemyTank();
            hitHero();

           this.repaint();
        }
    }
}
